
var Query = RichBase.extend({
	statics : {
		currentPage : 0,
		last : "",
		oData : {},
		queryState : {
			loading : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">加载数据...</p><img src="images/other/loading_1.gif" alt="" /></li>',
			empty : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">没有产品...</p></li>',
			fail : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求失败...</p></li>',
			unlogin : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">登录过期，请重新登录...</p></li>',
			timeout : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">网络请求超时...</p></li>',
			serverError : '<li class="loading" style="text-align:center; line-height:1; height:400px;"><p style="color:#aeaeae; font-size:12px; padding-top:300px;">请求出错...</p></li>'
		},
		getProv : function(id){
			var prov = "";
			var p = PROVINCES;
			if(!id) return prov;
			for(var i in p){
				var areaid = p[i]["area_id"];
				if(areaid==id){
					prov = p[i]["area_name"];
					break;
				}
			}
			return prov;
		},
		isObjEmpty : function(obj){
			for(var i in obj){
				return false;
			}
			return true;
		}
	},
	EVENTS : {
		"click" : {
			"#pagenavW .pageBtn" : "onPrevNextClick"
		}
	},
	init : function(opt){
		this.prevBtn = $("#prevPageBtn");
		this.nextBtn = $("#nextPageBtn");
	},
	setCurPage : function(page){
		this.statics.currentPage = page;
	},
	getCurPage : function(){
		return this.statics.currentPage;
	},
	getData : function(){
		return this.statics.oData;
	},
	setData : function(id,data){
		if(id && data){
			this.statics.oData[id] = data;
		}else{
			this.statics.oData = {};
		}
	},
	onPrevNextClick : function(that,e){
		var tarBtn = $(e.currentTarget);
		var first_ = Number($("#prevPageBtn").attr("data-first"));
		if(first_=="0"){
			that.prevBtn.addClass("disable");
		}else{
			that.prevBtn.removeClass("disable");
		}
		if(tarBtn.hasClass("disable")) return false;
		var type = $(".page_sub").attr("data");
		//var curPage = that.statics.currentPage;
		var curPage = $(".page_num_").val();
		var toPage = tarBtn.hasClass("prevBtn") ? (curPage-1) : (curPage+1);
		//console.log("dddd"+curPage)
		var all = $(".nextBtn").attr("total");
		var mid = Number($("#prevPageBtn").attr("data-mid"));
		var last_ = Number($("#prevPageBtn").attr("data-last"));
		
		var total_ = Number($("#nextPageBtn").attr("total"));
		//console.log("a"+toPage)
			if(tarBtn.hasClass("prevBtn")){
				//console.log("hui"+toPage)
				that.nextBtn.removeClass("disable");
				if(first_=="0"){
					that.prevBtn.addClass("disable");
					return false;
				}
				else{
					var searchData = sSearch.getData();
					qQuery.query({
						province : searchData.province,
						city : searchData.city,
						title : searchData.title,
						ty:1,
						supplier : searchData.supplier,
						currentPage : first_
					})
				}
				// if(toPage=="1"){
					// that.prevBtn.addClass("disable");
					// return false;
				// }
			}else{
				that.prevBtn.removeClass("disable");
				if(last_<=total_){
					var searchData = sSearch.getData();
					qQuery.query({
						province : searchData.province,
						city : searchData.city,
						title : searchData.title,
						ty:2,
						supplier : searchData.supplier,
						currentPage : last_
					})
				}else{
					//console.log("qi"+toPage)
					that.nextBtn.addClass("disable");
					//PFT_GLOBAL.U.Alert("fail",'<p style="width:240px">抱歉最后一页</p>');
				}
			}
			
	},
	query : function(opt){
		var that = this;
		var opt = opt || {};
		var pageSize = opt.pageSize || 15;
		var currentPage = opt.currentPage || "";
		var provice = opt.province || "";
		//console.log(provice)
		var city = opt.city || "";
		var title = opt.title || "";
		var ty = opt.ty || "2";
		//console.log(title)
		var supplier = opt.supplier || "";
		var ttimeout = opt.ttimeout || 20 * 60 * 1000;
		var data = {
			pageSize : pageSize,
			currentPage : currentPage,
			city : city,
			provice : provice,
			title : title,
			supplier : supplier,
			async : 1
		};
		var listUl = $("#mlistUl");
		var pagenavW = $("#pagenavW");
		var status = this.statics.queryState;
		if(ty=="2"){
		PFT.Ajax({
			//url : "product_package.html",
			url : "prod_sale_dt.html",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage += 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);} 
		},function(res){
			//console.log(res)
			var code = res.code;
			var lists = res.lists;
			var last = res.last;
			var totalPage = res.totalPage;
			
			//alert(code);
			if(code==200){
				if(!that.statics.isObjEmpty(lists)){
					//that.statics.oData[that.statics.currentPage] = res;
					//console.log(that.statics.oData[that.statics.currentPage])
					//alert("200");
					
					that.buildHtml(res);
				}else{
					listUl.html(status["empty"]);
					if(provice) that.fire("search_empty",res);
				}
				that.statics.last = last;
				if(!last){
					that.fire("no_next",res);
				}else{
					that.fire("has_next",res);
				}
			}else if(code==0){
				listUl.html(status["unlogin"]);
			}else{
				listUl.html(status["fail"]);
			}
		})
	}
		else{
			PFT.Ajax({
			url : "prod_sale_dt.html",
			type : "GET",
			dataType : "json",
			ttimeout : ttimeout,
			data : data,
			loading : function(){
				listUl.html(status["loading"]);
				pagenavW.hide();
			},
			removeLoading : function(res){
				that.statics.last = "";
				that.statics.currentPage -= 1;
				listUl.html("");
				pagenavW.show();
			},
			timeout : function(){ listUl.html(status["timeout"]);},
			serverError : function(){ listUl.html(status["serverError"]);} 
		},function(res){
			//console.log(res)
			var code = res.code;
			var lists = res.lists;
			var last = res.last;
			var totalPage = res.totalPage;
			
			//alert(code);
			if(code==200){
				if(!that.statics.isObjEmpty(lists)){
					//that.statics.oData[that.statics.currentPage] = res;
					//console.log(that.statics.oData[that.statics.currentPage])
					//alert("200");
					
					that.buildHtml(res);
				}else{
					listUl.html(status["empty"]);
					if(provice) that.fire("search_empty",res);
				}
				that.statics.last = last;
				if(!last){
					that.fire("no_next",res);
				}else{
					that.fire("has_next",res);
				}
			}else if(code==0){
				listUl.html(status["unlogin"]);
			}else{
				listUl.html(status["fail"]);
			}
		})
	}
	},
	buildHtml : function(res){
		var that = this;
		var code = res.code;
		var lists = res.lists;
		var str = "";
		var last = res.last;
		var currentPage = res.currentPage;
		var totalPage = res.totalPage;
		$(".nextBtn").attr("total",totalPage);
		$("#prevPageBtn").attr("data-mid",currentPage);
		$("#prevPageBtn").attr("data-last",currentPage+1);
		$("#prevPageBtn").attr("data-first",currentPage-1);
		//$(".total_num").html(totalPage)
		//$(".now_num").html(currentPage)
		// $(this).one("click", function(){
			// $(".prevBtn").attr("data-mid",totalPage);
		// });
		$("#prod_sale_tiptext").attr("data",totalPage);
		//$(".prevBtn").attr("data-mid",last);
		//var item_a = $("#itemTpl").html();
		//var item_div = $("#itemTpl_tr").html();
		//var item_con = $("#itemTpl_con").html();
		//var result = "";
		//var listUl = $("#mlistUl");
		//var pagenavW = $("#pagenavW");
		//lists = sData.adapt(lists);
		//console.log(lists)
		var data = lists;
		var url_type = $(".listWrap").attr("data_type");
		if(url_type=="1"){
			url="adjusting.html";
		}
		else{
			url="adjusting.html";
		}
		for(var i in data){
			is_data_empty = false;
			str += '<div class="list">';
			str += '<div class="list_top position_a ">';
			str +=     '<div class="title" data="'+data[i]["title"]+'" " title="'+data[i]["title"]+'">'+data[i]["title"]+'</div>';
			str += 			'<div class="top_right_a">';
			str +=				'<span class="supplier">供应商</span>';
			str +=				'<span class="supply_price">供货价</span>';
			str +=				'<span class="retail_price">零售价</span>';
			str +=				'<span class="operate">操作</span>';
			str +=			'</div>';
			str +=       '</div>';
			for(var s in data[i]["ticket"]){
				var t = data[i]["ticket"][s];
				var lists =data[i]["ticket"][s]["lists"];
				var tt = data[i]["ticket"][s]["title"];
				var pid = data[i]["ticket"][s]["pid"];
				var tid = t["pid"];
				str +=     '<div class="list_con" data-ac="">';
				str +=     '<div class="ticket"><span class="dtitle" title="'+tt+'">'+tt+'</span><span class="dtitl" title=""></span></div>';
				for(var j in lists){
					var dname=lists[j]["dname"];
					var js=lists[j]["js"];
					var ls=lists[j]["ls"];
					var id=lists[j]["aid"];
					var aids=lists[j]["apply_did"];
					var sid=lists[j]["sapply_did"];
					var active=lists[j]["active"];
					var lvl=lists[j]["lvl"];
					var conf_price=lists[j]["conf_price"];
					str +=     '<ul class="list_data">';
					str +=     '<li><span class="supplier_a" ><span class="title_name" title="'+dname+'">'+dname+'</span><span class="title_id" title="账户ID|景区ID|门票ID">'+data[i]["memberSID"]+'|'+i+'|'+s+'</span></span><span class="supply_price supply_data" data="'+js+'">'+js+'</span><span class="retail_price retail_data" data="'+ls+'">'+ls+'</span><span class="operate_a color_a operate_data" data="'+conf_price+'" onclick="window.open(\''+url+'?pid='+pid+'&sid='+sid+'&lvl='+lvl+'&aids='+aids+'\')">分销价格</span><span class="yujbtn" id="yuj" data-ac="" data-active="'+active+'"  data-conf_price="'+conf_price+'" data-show="'+conf_price+'" data-pid="'+pid+' "data-id="'+id+'"></span></li></ul>';
				}
				str +='</div>';
			}
			str += '</div>';
		}
		$("#mlistUl").html(str);
		$("#total_num").text("共"+totalPage+"页");	
		$("#whichPageNum").text(currentPage);
		$(".page_num_").val("");
		$(".yujbtn").each(function(){
			var active = Number($(this).attr("data-active"));
			if(active=="1"){
				if(conf_price=="1"){
					$(this).addClass("onyuj");
				}
				else{
					$(this).addClass("onyuj");
					$(this).parent().find(".color_a").html("");
				}
			}
			
		});
		
		$(".yujbtn").each(function(){
			var active = Number($(this).attr("data-active"));
			if(active=="0"){
				$(this).parent().find(".color_a").html("");
				//$(this).parent().find(".color_a").attr("onclick","white");
				
			}else{
				
				$(this).parent().find(".color_a").html("分销价格");
			}
		});
		$(".supply_data").each(function(){
			var pan = Number($(this).attr("data"));
			if(pan=="-1"){
				$(this).text("无供货价格");
			}
		})
		$(".retail_data").each(function(){
			var pan = Number($(this).attr("data"));
			if(pan=="-1"){
				$(this).text("无零售价格");
			}
		})
		$(".operate_data").each(function(){
			var pan = Number($(this).attr("data"));
			//console.log(pan)
			if(pan=="0"){
				$(this).text("");
			}
		})
		
	}
	
	
	
	
	
	
	
});





